<?php


class applicationEngine {
    
    private $page;
    private $pages = array();
    private $forms = array();
    private $functions = array();
    protected $defaultPage = 'defaulthome';
    protected $defaultErrorPage = 'error';
    private $requiredPage;
    private $requiredForm;
    private $requiredFunction;
    
    public function __construct() {
        
        # We startup our application    
        $this->init();
        
        # Try to determine what we are looking for
        $request = $this->getRequest();
        
        switch($request) {
            case 'form':
                $this->getRequiredForm();
                $this->executeForm();
                break;
            
            case 'ajax':
                $this->handleAjax();
                break;
            
            default:
                $this->handlePage();
                break;
        }
        
    }
    
    /*
     * @author Frank Roffel
     * @version 1.0 
     * @description start handling the page
     * 
     */
    private function handlePage() {
        
        # Try to locate the desired page
        $this->getRequiredPage();

    	try {
	        # Try to execute the required page
	        $this->executePage();
    	} catch (Exception $e) {
    		$this->pageError($e->getMessage());
    	}
    	
        # Create and display the page
        $this->page->generateTemplate();


    }
    
     /*
     * @author Frank Roffel
     * @version 1.0 
     * @description start handling the ajaxcall
     * 
     */
    private function handleAjax() {

        # Get the desired function
        $this->getRequiredAjax();
        
        # Try to execute the required page
        $this->executeAjax();

    }
    
    protected function init(){
        
        $this->addPage('defaulthome','$this->pageHome();');
        
    }
    
    
    /*
     * @author Frank Roffel
     * @version 1.0 
     * @description load a formfile to be used in our application
     * 
     */
    protected function loadForm($formFile, $page, $tag) {
        
        $this->requiredForm = $formFile;
        
        # The form needs the formengine, we add it here on the fly
        $page->addJavascriptFile('formEngine', true);
        
        $page = $this->executeForm($page, $tag);
        
        return $page;
    }
    
    /*
     * @author Frank Roffel
     * @version 1.0 
     * @description Determine what we need to do (form/page etc..)
     * 
     */
    private function getRequest() {
        
        if(isset($_GET['page'])) {
            return 'page';
        }
        
        if(isset($_GET['form'])) {
            return 'form';
        }
        
        if(isset($_GET['formsave'])) {
            return 'formsave';
        }
        
        if(isset($_GET['ajax'])) {
            return 'ajax';
        }
        
    }
    
    
    /*
     * @author Frank Roffel
     * @version 1.0 
     * @description Get the required page from the URL
     * 
     */
    private function getRequiredPage() {
        
        # In case no page is found go to the default page 
        $this->requiredPage = $this->defaultPage;
        
        # Try to get the desired page
        if(isset($_GET['page']) && $_GET['page'] != '') {
            $this->requiredPage = $_GET['page'];
        } 
        
    }
    
    
    /*
     * @author Frank Roffel
     * @version 1.0 
     * @description Get the required page from the URL
     * 
     */
    private function getRequiredForm() {
        
        # Try to get the desired page
        if(isset($_GET['form']) && $_GET['form'] != '') {
            $this->requiredForm = $_GET['form'];
        } 
        
    }
    
    
    
    /*
     * @author Frank Roffel
     * @version 1.0 
     * @description Get the required page from the URL
     * 
     */
    private function getRequiredAjax() {
        
        # Try to get the desired page
        if(isset($_GET['ajax']) && $_GET['ajax'] != '') {
            $this->requiredFunction = $_GET['ajax'];
        } 
        
    }
   
    /*
     * @author Frank Roffel
     * @version 1.0 
     * @description Try to execute the page in the application
     * 
     */
    private function executePage() {
        
        $pageFound = false;

        foreach($this->pages as $page) {
            if($page[0] == $this->requiredPage) {
                eval($page[1]);
                $pageFound = true;
            }
        }

        # TODO: als pagina niet gevonden is in de applicatie checken we de database of de pagina hierin voorkomt, dan runnen we deze

        # If the page isn't found, display the errorpage
        if($pageFound == false) {
            $this->pageError();
        }
        
    }
    
    
    
    /*
     * @author Frank Roffel
     * @version 1.0 
     * @description Try to execute the form 
     * 
     */
    private function executeForm($page = false, $tag = false) {
        
        # Check if the form exists
        foreach($this->forms as $form) {
            if($this->requiredForm == $form) {
                $form = $this->loadObject($form, $form, $page, $tag);
            }
        }
        
        if(!isset($form)) {
            echo 'Form ' . $this->requiredForm . ' is not found';
        } else {
            return $form;
        }
        
    }
    
    
    
    
     /*
     * @author Frank Roffel
     * @version 1.0 
     * @description Try to execute the desired function, but only if the function is open to ajax requests
     * 
     */
    private function executeAjax() {
        
        # Check if the required function is open to ajaxcalls
        foreach($this->functions as $function) {
            if($this->requiredFunction == $function[0]) {
                try {
                    eval($function[1]);
                } catch(Exception $e) {
                    # No warning given
                }
            }
        }
        
    }
    
    
    /*
     * @author Frank Roffel
     * @version 1.0 
     * @description Adds the page so we can access is when needed
     * 
     */
    public function addPage($pageName, $pageHandler) {
        $this->pages[] = array($pageName, $pageHandler);
    }
    
    
    
    /*
     * @author Frank Roffel
     * @version 1.0 
     * @description Adds a form so we can access it when needed
     * 
     */
    public function addForm($formName) {
        $this->forms[] = $formName;
    }
    
    
    
    /*
     * @author Frank Roffel
     * @version 1.0 
     * @description Adds the function to a list so we can acces it throught ajax
     * 
     */
    public function addFunction($functionName, $functionHandler) {
        $this->functions[] = array($functionName, $functionHandler);
    }
    
    /*
     * @author Frank Roffel
     * @version 1.1
     * @description Get an object out of the session or create a new one. Its static so it can be used everywhere
     * 
     */
    public static function loadObject($objectClass, $objectName, $page = false, $tag = false, $settings = array()) {
        
        if(isset($_SESSION[$objectName])){ 
            $object = unserialize($_SESSION[$objectName]);
        } else {
            $object = new $objectClass($objectName);
        }
        
        # TODO: alleen als object wordt aangemaakt?
        if($page && $tag) {
            if(method_exists($object, 'beforeInit')) {
                $object->beforeInit($page, $tag, $settings);
            }
        }
        
        $page = $object->init();
        
        if($page) {
         return $page;   
        } else {
         return $object;
        }
        
    }
    
    
    /*
     * @author Frank Roffel
     * @version 1.0 
     * @description Find the object within the Session and return it
     * 
     */
    public static function findObject($objectName) {
        
        if(!isset($_SESSION[$objectName])){ 
            return false;
        } 
        
        $object = unserialize($_SESSION[$objectName]);
        
        return $object;
    }
    
    
    /*
     * @author Frank Roffel
     * @version 1.0 
     * @description Adds default javascriptfiles to the page
     * 
     */
    private function getDefaultJavascripts($page) {
        
        $page->addJavascriptFile('jquery/jquery.'.$GLOBALS['jqueryVersion'], true);
        $page->addJavascriptFile('functions', true);
        $page->addJavascriptFile('displayEngine', true);
        $page->addJavascriptFile('jquery/jquery.colorbox/js/jquery.colorbox', true);
        $page->addJavascriptFile('modalEngine', true);
        
        return $page;
        
    }
    
    /*
     * @author Frank Roffel
     * @version 1.0 
     * @description Adds default css to the page
     * 
     */
    private function getDefaultCss($page) {
        
        $page->addCssFile('default',true);
        $page->addCssFile('../javascript/jquery/jquery.colorbox/colorbox',true);
        
        return $page;
        
    }
    
    /*
     * @author Frank Roffel
     * @version 1.0 
     * @description Creates the page so we can add html etc...
     * 
     */
    public function createMainPage() {
        
        # Create the default template to add al other html, templates etc...
        $this->page = new templateEngine();
        
        # Add all default javascripts needed
        $this->page = $this->getDefaultJavascripts($this->page);
        
        # Add all default css needed
        $this->page = $this->getDefaultCss($this->page);
        
        return $this->page;
        
    }
    
    /*
     * @author Frank Roffel
     * @version 1.0 
     * @description Creates a page to be used within the colorbox
     * 
     */
    public function createIframePage() {
        
        $this->page = new templateEngine();
        
        # Add all default javascripts needed
        $this->page = $this->getDefaultJavascripts($this->page);
        
        $this->page->addCssFile('default',true);
        
        return $this->page;
        
    }
    
    /*
     * @author Frank Roffel
     * @version 1.0 
     * @description Our default homepage, this method should be overruled by the application
     * 
     */
    protected function pageHome() {
        
        $page = $this->createMainPage();
        
        $page->assign('You do not have a default homepage, create on first','mainContent');
        
    }
    
    /*
     * @author Frank Roffel
     * @version 1.0 
     * @description Display the default errorpage, this method can be overruled
     * 
     */
    protected function pageError($errorMsg) {
        
        $page = $this->createMainPage();
        
        $page->addCssFile('defaulterrorpage',true);
        $page->assignTemplate('defaulterrorpage','mainContent',true);
        
    }
    
}

?>
